package com.zking.easyuidemo.dao;

import java.util.ArrayList;
import java.util.List;

import org.junit.Test;
import org.lisen.mvc.util.DbTemplate;
import org.lisen.mvc.util.PageBean;

import com.mysql.jdbc.StringUtils;
import com.zking.easyuidemo.model.Book;

public class BookDao {
	
	public List<Book> listBooks(Book book, PageBean pageBean) {
		
		String sql = "select * from t_book where 1=1 ";
		
		List<Object> param  = new ArrayList<>();
		if(book != null && !StringUtils.isNullOrEmpty(book.getBookName())) {
			sql += " and book_name like ?";
			param.add(book.getBookName()+"%");
		}
		
		return DbTemplate.query(sql, param.toArray(), pageBean, Book.class);
	}
	
	
	public void updateBook(Book book) {
		DbTemplate.update(book);
	}
	
	public void addBook(Book book) {
		DbTemplate.save(book);
	}
	
	@Test
	public void testListBooks() {
		
		Book book = new Book();
		PageBean pageBean = new PageBean();
		
		List<Book> books = listBooks(book,pageBean);
		books.forEach(t -> System.out.println(t));
	}

}
